//
//  DoctorConsulation.m
//  medicalConsultation
//
//  Created by apple on 16/5/6.
//  Copyright © 2016年 Hill. All rights reserved.
//

//define this constant if you want to use Masonry without the 'mas_' prefix
#define MAS_SHORTHAND

//define this constant if you want to enable auto-boxing for default syntax
#define MAS_SHORTHAND_GLOBALS

#import "Masonry.h"

#import "AYMDoctorConsulation.h"

#import "AYMFocusDoctorModel.h"

#import <UIImageView+WebCache.h>
@interface AYMDoctorConsulation()<UIScrollViewDelegate>

/**
 *  锦旗量
 */
@property (nonatomic, weak) UIButton *btn_silkFlag;
/**
 *  鲜花量
 */
@property (nonatomic, weak) UIButton *btn_flowerCount;
/**
 *  预约量
 */
@property (nonatomic, weak) UIButton *btn_appointment;
/**
 *  医生科室
 */
@property (nonatomic, weak) UILabel *lb_doctorDepartment;
/**
 *  医生职称
 */
@property (nonatomic, weak) UILabel *lb_doctorType;
/**
 *  医生名字
 */
@property (nonatomic, weak) UILabel *lb_doctorName;
/**
 *  医生头像
 */
@property (nonatomic, weak) UIImageView *igv_iconImage;
/**
 *  滚动界面
 */
@property (nonatomic, weak) UIScrollView *scroll;
/**
 *  滚动条
 */
@property (nonatomic, weak) UIView *scrollLine;

/**
 *  选中的按钮
 */
@property (nonatomic, weak) UIButton *btn_lastSelected;

/**
 *  咨询按钮的父容器
 */
@property (nonatomic, weak) UIView *consultation;

/**
 *  滚动按钮控件
 */
@property (nonatomic, weak) UIView *scrollBtnView;

/**
 *  功能按钮视图
 */
@property (nonatomic, strong) UIView *functionView;

/**
 *  接诊条件按钮
 */
@property (nonatomic, weak) UIButton *btn_requirement;

/**
 *  医生简介按钮
 */
@property (nonatomic, weak) UIButton *btn_introduction;


@end

static NSString *identifier = @"doctorConsulationCell";

@implementation AYMDoctorConsulation

- (void)setModel:(AYMFocusDoctorModel *)model {
    
    _model = model;
    
    Log(@"%@",model);
    //    设置医生名称
    self.lb_doctorName.text = model.doctor_name;
    //    设置医生职称
    self.lb_doctorType.text = model.doctor_title_name;
    //    设置医生头像
    [self.igv_iconImage sd_setImageWithURL:[NSURL URLWithString:model.doctor_portrait]];
    //    设置医院名称
    self.lb_doctorDepartment.text = model.hospital_name;
    //    设置预约数量
    [self.btn_appointment setTitle:[NSString stringWithFormat:@"预约量:%@",model.operation_count] forState:UIControlStateNormal];
    //    设置鲜花数量
    [self.btn_flowerCount setTitle:[NSString stringWithFormat:@"鲜花量:%@",model.flower] forState:UIControlStateNormal];
    //    设置锦旗数量
    [self.btn_silkFlag setTitle:[NSString stringWithFormat:@"锦旗量:%@",model.banner] forState:UIControlStateNormal];
    
    
    
}

- (instancetype)init {
    
    if (self = [super init]) {
        
        
        [self setupUI];
        
    }
    
    return  self;
}

- (void)setupUI {
    
    self.tag = 20000;

    
    
}

-(void)layoutSubviews{
    
    // 设置顶部介绍视图
    [self setupHeadIntroduction];
    
    // 设置接诊按钮和简介按钮视图
    [self setupScrollButton];
    // 设置底部咨询按钮视图
    [self setDoctorConsultationBtn];
    // 设置滚动界面
    [self setupScrollView];
    
    // 设置滚动条
    [self setScrollLine];
    
    
}
# pragma mark -
# pragma mark 设置顶部介绍视图
- (void) setupHeadIntroduction {
    
    // 顶部医生介绍整体
    UIView *headerIntroduce = [[UIView alloc] init];
    
    
    [self addSubview:headerIntroduce];
    
    
    // 医生介绍
    UIView *doctorIntroduce = [[UIView alloc] init];
    
    // 医生头像
    UIImageView *igv_iconImage = [[UIImageView alloc] init];
    [doctorIntroduce addSubview:igv_iconImage];
    
    self.igv_iconImage = igv_iconImage;
    
    
    // 医生姓名
    UILabel *lb_doctorName = [[UILabel alloc] init];
    
    [doctorIntroduce addSubview:lb_doctorName];
    self.lb_doctorName = lb_doctorName;
    
    //    lb_doctorName.text = @"华佗";
    lb_doctorName.font = [UIFont systemFontOfSize:13];
    
    
    // 医生类型
    UILabel *lb_doctorType = [[UILabel alloc] init];
    self.lb_doctorType = lb_doctorType;
    //    lb_doctorType.text = @"内科医生";
    lb_doctorType.tintColor = [UIColor darkGrayColor];
    lb_doctorType.font = [UIFont systemFontOfSize:12];
    
    [doctorIntroduce addSubview:lb_doctorType];
    
    // 医生科室
    UILabel *lb_doctorDepartment = [[UILabel alloc] init];
    
    self.lb_doctorDepartment = lb_doctorDepartment;
    //    lb_doctorDepartment.text = @"上海阿波罗医院-内科医院";
    lb_doctorDepartment.tintColor = [UIColor lightGrayColor];
    lb_doctorDepartment.font = [UIFont systemFontOfSize:13];
    
    [doctorIntroduce addSubview:lb_doctorDepartment];
    
    // 医生头像约束
    [igv_iconImage makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(doctorIntroduce).offset(10);
        make.leading.equalTo(doctorIntroduce).offset(10);
        make.height.equalTo(60);
        make.width.equalTo(60);
        
    }];
    
    // 医生姓名约束
    [lb_doctorName makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(igv_iconImage);
        make.leading.equalTo(igv_iconImage.mas_trailing).offset(10);
        
    }];
    
    // 医生类型约束
    [lb_doctorType makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(lb_doctorName);
        make.leading.equalTo(lb_doctorName.mas_trailing).offset(10);
        
    }];
    
    // 医生科室约束
    [lb_doctorDepartment makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(igv_iconImage);
        make.leading.equalTo(lb_doctorName);
        
    }];
    
    // 将医生简介的页面提交添加到整个view上
    [headerIntroduce addSubview:doctorIntroduce];
    
    // 功能按钮视图
    UIView *functionView = [[UIView alloc] init];
    self.functionView = functionView;
    
    // 预约量
    UIButton *btn_appointment = [[UIButton alloc] init];
    
    self.btn_appointment = btn_appointment;
    
    [btn_appointment setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    
    [btn_appointment setImage:[UIImage imageNamed:@"yuyueliang"] forState:UIControlStateNormal];
    
    btn_appointment.titleLabel.font = [UIFont systemFontOfSize:13];
    
    
    [functionView addSubview:btn_appointment];
    
    // 鲜花量
    UIButton *btn_flowerCount = [[UIButton alloc] init];
    
    self.btn_flowerCount = btn_flowerCount;
    
    [btn_flowerCount setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    
    [btn_flowerCount setImage:[UIImage imageNamed:@"xianhua"] forState:UIControlStateNormal];
    
    
    btn_flowerCount.titleLabel.font = [UIFont systemFontOfSize:13];
    
    [functionView addSubview:btn_flowerCount];
    
    // 锦旗量
    UIButton *btn_silkFlag = [[UIButton alloc] init];
    self.btn_silkFlag = btn_silkFlag;
    
    [btn_silkFlag setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    
    [btn_silkFlag setImage:[UIImage imageNamed:@"jinqi"] forState:UIControlStateNormal];
    
    btn_silkFlag.titleLabel.font = [UIFont systemFontOfSize:13];
    
    [functionView addSubview:btn_silkFlag];
    
    // 预约量的约束
    [btn_appointment makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(functionView);
        make.leading.equalTo(functionView);
        make.width.equalTo(btn_flowerCount);
        
    }];
    // 鲜花量的约束
    [btn_flowerCount makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(btn_appointment);
        make.width.equalTo(btn_silkFlag);
        make.leading.equalTo(btn_appointment.mas_trailing);
        
    }];
    // 锦旗量的约束
    [btn_silkFlag makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(btn_flowerCount);
        make.leading.equalTo(btn_flowerCount.mas_trailing);
        make.trailing.equalTo(functionView);
        make.width.equalTo(btn_appointment);
        
    }];
    
    [headerIntroduce addSubview:functionView];
    
    // 功能按钮栏的约束
    [functionView makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(igv_iconImage.mas_bottom).offset(10);
        make.leading.equalTo(self);
        make.trailing.equalTo(self);
        make.height.equalTo(40);
    }];
    
}



# pragma mark -
# pragma mark 接诊按钮和简介按钮的视图
- (void) setupScrollButton {
    
    // 滚动按钮的底部view
    UIView *scrollBtnView = [[UIView alloc] init];
    
    self.scrollBtnView = scrollBtnView;
    
    [self addSubview:scrollBtnView];
    
    [scrollBtnView makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.functionView.mas_bottom).offset(5);
        make.leading.equalTo(self);
        make.trailing.equalTo(self);
        make.height.equalTo(40);
        
    }];
    
    [scrollBtnView layoutIfNeeded];
    
    // 接诊条件
    UIButton *btn_requirement = [[UIButton alloc] init];
    
    btn_requirement.tag = 10;
    self.btn_requirement = btn_requirement;
    [btn_requirement addTarget:self action:@selector(clickScorllButton:) forControlEvents:UIControlEventTouchUpInside];
    
    self.btn_lastSelected = btn_requirement;
    
    [btn_requirement setTitle:@"接诊条件" forState:UIControlStateNormal];
    [btn_requirement setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [btn_requirement setTitleColor:commonTextColor forState:UIControlStateSelected];
    
    [scrollBtnView addSubview:btn_requirement];
    
    
    // 医生简介
    UIButton *btn_introduction = [[UIButton alloc] init];
    self.btn_introduction = btn_introduction;
    
    btn_introduction.tag = 20;
    [btn_introduction addTarget:self action:@selector(clickScorllButton:) forControlEvents:UIControlEventTouchUpInside];
    [btn_introduction setTitle:@"医生简介" forState:UIControlStateNormal];
    [btn_introduction setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [btn_introduction setTitleColor:commonTextColor forState:UIControlStateSelected];
    
    [scrollBtnView addSubview:btn_introduction];
    
    // 中间那条竖线
    UIView *verticalLine = [[UIView alloc] init];
    
    verticalLine.backgroundColor = [UIColor blackColor];
    
    [scrollBtnView addSubview:verticalLine];
    
    // 接诊按钮的约束()
    [btn_requirement makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(scrollBtnView);
        make.bottom.equalTo(scrollBtnView).offset(-2);
        make.leading.equalTo(scrollBtnView);
        //        make.width.equalTo(btn_introduction);
        make.width.equalTo(self.scrollBtnView.frame.size.width * 0.5);
        
    }];
    
    // 医生简介按钮的约束
    [btn_introduction makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(btn_requirement);
        make.trailing.equalTo(scrollBtnView);
        make.bottom.equalTo(scrollBtnView).offset(-2);
        make.width.mas_equalTo(screenW*0.5);
        
    }];
    
    // 分割线的约束
    [verticalLine makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(scrollBtnView);
        make.top.equalTo(scrollBtnView).offset(5);
        make.bottom.equalTo(scrollBtnView).offset(-5);
        make.width.equalTo(1);
    }];
    
    [btn_introduction layoutIfNeeded];
    [btn_requirement layoutIfNeeded];
    [verticalLine layoutIfNeeded];
    
    
    
}

# pragma mark -
# pragma mark 创建滚动条
- (void)setScrollLine{
    
    UIView *scrollLine = [[UIView alloc] init];
    self.scrollLine = scrollLine;
    
    scrollLine.backgroundColor = commonTextColor;
    
    [self.scrollBtnView addSubview:scrollLine];
    
    self.scrollLine.frame = CGRectMake(self.scrollBtnView.frame.size.width * 0.05, CGRectGetMaxY(self.btn_requirement.frame) , self.scrollBtnView.frame.size.width * 0.40 , 2);
    
    
}


# pragma mark -
# pragma mark 设置滚动视图
- (void)setupScrollView{
    
    UIScrollView *scroll = [[UIScrollView alloc] init];
    
    [self addSubview:scroll];
    
    self.scroll = scroll;
    
    scroll.delegate = self;
    
    [scroll makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.scrollBtnView.mas_bottom).offset(5);
        make.leading.trailing.equalTo(self);
        make.bottom.equalTo(self.consultation.top);
        
    }];
    
    // 接诊条件上半部分
    UILabel *lb_requirementUp = [[UILabel alloc] init];
    
    lb_requirementUp.text = @"山东矿机上课就擦拭的挥洒佳洁士肯德基卡上的卡位还不算大空间黄金时刻回到家撒谎的健康撒谎的即可撒谎的健康暗杀收货订金卡萨丁卡说假话的健康爱好的空间安徽省健康的哈桑萨哈空间等哈就开始对";
    lb_requirementUp.textColor = [UIColor orangeColor];
    lb_requirementUp.numberOfLines = 0;
    
    [scroll addSubview:lb_requirementUp];
    
    [lb_requirementUp makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(scroll).offset(10);
        make.leading.equalTo(scroll).offset(20);
        make.width.equalTo(screenW - 40);
        
    }];
    
    // 接诊条件下半部分
    UILabel *lb_requirementDown = [[UILabel alloc] init];
    lb_requirementDown.text = @"哈哈哈是的挥洒接电话是骄傲的哈哈说句话大神就带回家爱神的箭案件蝴蝶结爱神的箭撒谎就动画是爱是件大事大声道按实际的哈时间段按时间段撒娇按时间段和骄傲是混搭神剧暗杀大红色的哈斯就 回到家圣诞节房间爱垃圾堆里卡坚持就死定了节日快乐家里的可使肌肤及农产品提偶强耦合的不可复活点上课哟呵火锅底料祸害谁厉害的绿化办起来人的快乐和分开算厉害了不合理韩日v；熊";
    lb_requirementDown.numberOfLines = 0;
    [scroll addSubview:lb_requirementDown];
    
    [lb_requirementDown makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(lb_requirementUp.mas_bottom).offset(10);
        make.leading.trailing.equalTo(lb_requirementUp);
        
    }];
    
    // 简介label
    UILabel *lb_introduction = [[UILabel alloc] init];
    
    lb_introduction.text = @"大家打开数据库来得及奥斯卡的脸颊上肯德基萨克了解到快乐撒娇的抗裂砂浆就打开垃圾打开了数据库的叫撒快乐的骄傲是快乐就分开垃圾罚款洛杉矶快乐的骄傲是考虑到就撒了空间的啦接收到看撒娇的拉时间段里看见阿斯利康的大手大脚阿克苏来得及刷卡机读卡涉及到快乐时间段可垃圾思维的骄傲是快乐的激情我觉得老卡死机第六届加点蓝精灵咔擦建设路口家里快到家了卡时间段都卡死了新纳粹UR骗回复的发挥还打呼噜打飞机卡入大都市加入房间按揭贷款拉萨酒店可拉伸机打开了捡垃圾的卡萨了解到快乐解放了看见了就发了就垃圾来接你了几年了就爱了吃了撒今年的来看车龙卡及离开的才能破解来得及发垃圾率金额爱离开v离开";
    
    lb_introduction.numberOfLines = 0;
    [scroll addSubview:lb_introduction];
    
    [lb_introduction makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(scroll).offset(10);
        make.leading.equalTo(lb_requirementUp.mas_trailing).offset(40);
        make.width.equalTo(screenW - 40);
        
    }];
    
    scroll.userInteractionEnabled = YES;
    scroll.contentSize = CGSizeMake(screenW * 2, scroll.frame.size.height);
    scroll.bounces = NO;
    scroll.pagingEnabled = YES;
    scroll.showsHorizontalScrollIndicator = NO;
    
    [scroll layoutIfNeeded];
    [lb_introduction layoutIfNeeded];
    [lb_requirementDown layoutIfNeeded];
    [lb_requirementUp layoutIfNeeded];
    
}


# pragma mark -
# pragma mark 咨询医生按钮
- (void)setDoctorConsultationBtn{
    // 按钮视图
    UIView *consultation = [[UIView alloc] init];
    
    self.consultation = consultation;
    [self addSubview:consultation];
    // 按钮
    UIButton *btn_consultation = [[UIButton alloc] init];
    
    [btn_consultation addTarget:self action:@selector(consultationButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [btn_consultation setTitle:@"咨询医生" forState:UIControlStateNormal];
    [btn_consultation setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn_consultation setBackgroundColor:commonTextColor];
    
    [consultation addSubview:btn_consultation];
    
    // 按钮视图约束
    [consultation makeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.trailing.equalTo(self);
        make.bottom.equalTo(self).offset(-70);
        make.height.equalTo(50);
        
    }];
    
    // 按钮约束
    [btn_consultation makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(consultation);
        make.leading.equalTo(consultation).offset(20);
        make.trailing.equalTo(consultation).offset(-20);
        
    }];
    
}



# pragma mark -
# pragma mark 响应按钮点击
//接诊状态按钮响应方法
- (void)clickScorllButton:(UIButton *)button {
    
    
    if (button.tag == 10) {
        
        self.btn_requirement.selected = YES;
        self.btn_introduction.selected = NO;
        self.btn_lastSelected = button;
        Log(@"scrollBtnView 11  %f",CGRectGetMaxY(self.scrollBtnView.frame));
        // 点击移动滑动条
        [UIView animateWithDuration:1.0 animations:^{
            
            self.scrollLine.frame = CGRectMake(self.scrollBtnView.frame.size.width * 0.05, CGRectGetMaxY(self.btn_lastSelected.frame) , self.scrollBtnView.frame.size.width * 0.40 , 2);
            
            [self.scroll setContentOffset:CGPointMake(0 , 0)];
        }];
        
        
        [self layoutIfNeeded];
        
        
    }else if(button.tag == 20){
        
        self.btn_requirement.selected = NO;
        self.btn_introduction.selected = YES;
        self.btn_lastSelected = button;
        Log(@"scrollBtnView 22= %f",CGRectGetMaxY(self.scrollBtnView.frame));
        
        [UIView animateWithDuration:1.0 animations:^{
            
            self.scrollLine.frame = CGRectMake(self.scrollBtnView.frame.size.width * 0.55, CGRectGetMaxY(self.btn_lastSelected.frame) , self.scrollBtnView.frame.size.width * 0.40 , 2);
            [self.scroll setContentOffset:CGPointMake(screenW, 0)];
        }];
        
        
    }
    
    
    
}




- (void)consultationButtonClick:(UIButton *)button {
    
    if ([self.delegate respondsToSelector:@selector(doctorConsulation:)]) {
        
        [self.delegate doctorConsulation:self];
    }
    
}


# pragma mark -
# pragma mark 代理方法

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    // 如果scrollView偏移量等于0 ， 按钮停在接诊条件
    if (scrollView.contentOffset.x == 0) {
        
        self.btn_requirement.selected = YES;
        self.btn_introduction.selected = NO;
        
        
        [UIView animateWithDuration:1.0 animations:^{
            
            self.scrollLine.frame = CGRectMake(0, CGRectGetMaxY(self.btn_lastSelected.frame) , self.scrollBtnView.frame.size.width * 0.45 , 2);
        }];
        
        // 如果偏移量等于一个屏幕的宽度，按钮停在医生简介
    }else if (scrollView.contentOffset.x == screenW ){
        self.btn_requirement.selected = NO;
        self.btn_introduction.selected = YES;
        
        
        
        [UIView animateWithDuration:1.0 animations:^{
            
            self.scrollLine.frame = CGRectMake(self.scrollBtnView.frame.size.width * 0.55, CGRectGetMaxY(self.btn_lastSelected.frame) , self.scrollBtnView.frame.size.width * 0.45 , 2);
        }];
        
    }
    
}





@end
